package reflect;

public class JDK8JavaArrayMethodNewInstance {


    public static void main(String[] args) throws ClassNotFoundException {
        argumentOfComponentType();

    }

    /**
     * Class<?> 获取 componentType参数
     * @throws ClassNotFoundException
     */
    private static void argumentOfComponentType() throws ClassNotFoundException {
        // 基本数据类型
        System.out.println("基本数据类型例子--------------------");
        System.out.println(int.class);
        System.out.println("基本数据类型例子--------------------");
        System.out.println();

        // 引用类型
        System.out.println("引用类型例子--------------------");
        // 类
        System.out.println(String.class);
        // 类名
        System.out.println(Class.forName("java.lang.String").toString());
        // 对象
        System.out.println("".getClass());
        System.out.println("引用类型例子--------------------");
        System.out.println();

        // 自定义的类，参照{@link JDK8JavaObjectMethodGetClass}

        String strObject = "0";
        System.out.println("StringObject--------------------------");
        System.out.println(strObject.getClass());
        System.out.println("StringObject--------------------------");
        System.out.println();

        // 引用类型但不是数组 getClass()
        // 是数组 就加 getComponentType()，几维数组就加几个 getComponentType()
        // oneDimensionalArray
        // twoDimensionalArray
        // threeDimensionalArray

        String[] oneDimensionalArray = {"1","2"};

        System.out.println("oneDimensionalArray--------------------------");

        // class java.lang.String
        System.out.println(oneDimensionalArray.getClass().getComponentType());
        // null
        System.out.println(oneDimensionalArray.getClass().getComponentType().getComponentType());
        // 再往下就会空指针 java.lang.NullPointerException
//        System.out.println(oneDimensionalArray.getClass().getComponentType().getComponentType().getComponentType());

        System.out.println("oneDimensionalArray--------------------------");
        System.out.println();


        String[][] twoDimensionalArray = {{"1"},{"1","2"}};

        System.out.println("twoDimensionalArray--------------------------");
        // class [[Ljava.lang.String;
        System.out.println(twoDimensionalArray.getClass());
        // class [Ljava.lang.String;
        System.out.println(twoDimensionalArray.getClass().getComponentType());
        // class java.lang.String
        System.out.println(twoDimensionalArray.getClass().getComponentType().getComponentType());
        System.out.println("twoDimensionalArray--------------------------");
        System.out.println();


        String[][][] threeDimensionalArray = {
                {{"1"},{"1","2"}}
        };

        System.out.println("threeDimensionalArray--------------------------");
        // class [[[Ljava.lang.String;
        System.out.println(threeDimensionalArray.getClass());
        // class [[Ljava.lang.String;
        System.out.println(threeDimensionalArray.getClass().getComponentType());
        // class [Ljava.lang.String;
        System.out.println(threeDimensionalArray.getClass().getComponentType().getComponentType());
        // class java.lang.String
        System.out.println(threeDimensionalArray.getClass().getComponentType().getComponentType().getComponentType());
        System.out.println("threeDimensionalArray--------------------------");
        System.out.println();
    }


        /*
    *

输出结果：

StringObject--------------------------
class java.lang.String
StringObject--------------------------

oneDimensionalArray--------------------------
class java.lang.String
null
oneDimensionalArray--------------------------

twoDimensionalArray--------------------------
class [[Ljava.lang.String;
class [Ljava.lang.String;
class java.lang.String
twoDimensionalArray--------------------------

threeDimensionalArray--------------------------
class [[[Ljava.lang.String;
class [[Ljava.lang.String;
class [Ljava.lang.String;
class java.lang.String
threeDimensionalArray--------------------------

    * */
}
